package calao.client;

import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;

import calao.client.i18n.Text;
import calao.client.ui.Form;
import calao.client.ui.HasLinks;
import calao.client.ui.HasTitle;
import calao.client.ui.Link;
import calao.client.ui.TabPanel;
import calao.client.ui.NumberedListBox;

public class MemberPanel
extends TabPanel
implements HasTitle, HasLinks
{
	
	class MainForm extends Form 
	{
		public void reset()
		{
			nameBox.setText(info.name);
			aliasBox.setText(info.alias);
			emailBox.setText(info.email);
			profilesBox.setNumberSelected(info.profiles);
			profilesBox.refresh();
			inactiveBox.setChecked(info.isInactive());
		}
		
		public void saveChanges()
		{
			info.setAlias(aliasBox.getText());
			info.setEmail(emailBox.getText());
			info.setProfiles(profilesBox.getSelectedList());
			info.setName(nameBox.getText());
			info.setInactive(inactiveBox.isChecked());
			service.modify(info, new ServiceCallback() {
				protected void onReturn(Object result) {
					mainForm.setChangesSaved();
					parent.refresh();
				}
			});
		}		
	}
	
	class PasswordForm extends Form 
	implements HasTitle 
	{
		final PasswordTextBox passwdBox = new PasswordTextBox();
		final PasswordTextBox confirmBox = new PasswordTextBox();
		PasswordForm() 
		{
			add(text.password(), passwdBox, null);
			add(text.confirm(), confirmBox, null);
		}
		public void saveChanges() 
		{
			if (passwdBox.getText().equals(confirmBox.getText())) {
				MemberInfo modif = new MemberInfo(info.guid);
				modif.setPassword(passwdBox.getText());
				service.modify(modif, this.newSaveChangesCallback());
			}
			else {
				Desktop.getDesktop().setRedStatus(text.passwordNotConfirmed());
			}
		}
		public String getTitle() 
		{
			return text.changePassword();
		}
	}
	
	static final Text text = Desktop.getDesktop().text;
	
	final MemberServiceAsync service = Desktop.getDesktop().memberService;
	final MainForm mainForm = new MainForm();
	final TextBox nameBox = new TextBox();
	final TextBox aliasBox = new TextBox();
	final TextBox emailBox = new TextBox();
	final CheckBox inactiveBox = new CheckBox();
	final NumberedListBox profilesBox = new NumberedListBox(NumberedListBox.CHECK_LIST) {
		protected int getItemCount() {
			return profileInfo.length;
		}
		protected long getItemNumber(int index) {
			return profileInfo[index].id;
		}
		protected String getItemText(int index) {
			return profileInfo[index].name;
		}
	};

	final UserAdminPanel parent;
	final ProfileInfo[] profileInfo;
	MemberInfo info;
	
	MemberPanel(UserAdminPanel parent, MemberInfo info)
	{
		this.parent = parent;
		this.info = info;
		this.profileInfo = parent.profileInfo;
		mainForm.add(text.name(), nameBox);
		mainForm.add(text.userAlias(), aliasBox);
		mainForm.add(text.email(), emailBox);
		mainForm.add(text.profiles(), profilesBox);
		mainForm.add(text.inactive(), inactiveBox);
		newTab(text.general(), mainForm);
		service.detailMember(info.guid, new ServiceCallback() {
			protected void onReturn(Object result) {
				MemberPanel.this.info = (MemberInfo)result;
				mainForm.reset();
			}
		});
	}
			
	public String getTitle()
	{
		return info.getName();
	}

	public Link[] getLinks() 
	{
		return new Link[] {
				new Link(text.changePassword()) {
					protected void onClick() {
						Desktop.getDesktop().stackView(new PasswordForm());
					}
				}
		};
	}
	
	public Form getMainForm()
	{
		return mainForm;
	}
	
	public MemberInfo getMember()
	{
		return info;
	}

}
